 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=200 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:29 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Try pseudo-persistence for mock ups
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='DAO,database,persistence,stub,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Try pseudo-persistence for mock ups</div>

<div class='main-body'>
 
<br>It is possible to build <a href="TopicAction564b-2.html">Data Access Objects</a>
which mimic persistance, but whose data lasts only until the application
shuts down. One might call this "pseudo-persistance", and such objects
"pseudo-DAOs".
<p>These stub classes are easy to construct, and can be useful for quick
mock ups. They require less effort than providing true persistance. By
following the style shown below, for example, a pseudo-DAO could be constructed
in 15 minutes.
<p>This example pseudo-DAO implements pseudo-persistence for <tt>Person</tt>
objects. The intent is to create a pseudo-DAO whose API will match the
final, true DAO. Thus, the same class can evolve from a mock up into a
final&nbsp; implementation, without affecting the caller in any way.
<p>The underlying technique is simply to store objects in a static member
of the pseudo-DAO. In this case, a <tt>Map</tt> is used.
<br>
<PRE>

<span class='keyword'>package</span> myapp.data;

<span class='keyword'>import</span> java.util.*;

<span class='keyword'>import</span> myapp.business.Person;

<span class='comment'>/**
* Provides "pseudo-persistance", by storing objects as static data attached to this
* class. Such data is lost when the application is shut down, but is retained
* as long as the application is running.
*
* &lt;P&gt;An alternate style would be to use static, synchronized methods. This would
* be appropriate if the DAO implementation is used directly by the caller, as in
* &lt;tt&gt;PersonDAO.fetch(name)&lt;/tt&gt;, instead of via a factory. The important thing
* is to match the desired style of the true DAO into which this pseudo-DAO
* will evolve.
*
* &lt;P&gt;Note that &lt;tt&gt;DataAccessException&lt;/tt&gt; (a checked exception) can and should
* be declared in the throws clause, even when the underlying implementation for
* this pseudo-DAO never throws such an exception. Again, the idea is to provide
* a stub which, from the point of view of the caller, is as realistic as possible.
*/</span>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> PersonDAO {

  <span class='comment'>/**
  * Add &lt;tt&gt;aPerson&lt;/tt&gt; to the datastore, returning an indicator of
  * success or failure.
  *
  * If &lt;tt&gt;aPerson&lt;/tt&gt; already exists in the datastore, then return false.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>boolean</span> add(Person aPerson) <span class='keyword'>throws</span> DataAccessException {
    <span class='keyword'>boolean</span> result = <span class='keyword'>false</span>;
    <span class='keyword'>synchronized</span>(fPersons) {
      <span class='keyword'>if</span> ( ! fPersons.containsKey(aPerson.getName()) ) {
        fPersons.put(aPerson.getName(), aPerson);
        result = <span class='keyword'>true</span>;
        log(<span class='literal'>"Persons after add: "</span> + fPersons);
      }
    }
    <span class='keyword'>return</span> result;
  }

  <span class='comment'>/**
  * Fetch a person from the datastore, using &lt;tt&gt;aName&lt;/tt&gt; as a unique
  * identifier.
  *
  * &lt;P&gt;If no &lt;tt&gt;Person&lt;/tt&gt; is found, return null.
  */</span>
  <span class='keyword'>public</span> Person fetch(String aName) <span class='keyword'>throws</span> DataAccessException {
    Person result = <span class='keyword'>null</span>;
    <span class='keyword'>synchronized</span>(fPersons){
      <span class='keyword'>if</span>( fPersons.containsKey(aName) ){
        result = (Person)fPersons.get(aName);
      }
      log(<span class='literal'>"Persons during fetch: "</span> + fPersons);
    }
    <span class='keyword'>return</span> result;
  }

  <span class='comment'>/**
  * Update the data associated with &lt;tt&gt;aPersons&lt;/tt&gt;, returning an
  * indicator of success or failure.
  *
  * &lt;P&gt;If &lt;tt&gt;aPerson&lt;/tt&gt; is not found in the datastore, then return false.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>boolean</span> change(Person aPerson) <span class='keyword'>throws</span> DataAccessException {
    <span class='keyword'>boolean</span> result = <span class='keyword'>false</span>;
    <span class='keyword'>synchronized</span>(fPersons){
      <span class='keyword'>if</span> ( fPersons.containsKey(aPerson.getName()) ) {
        fPersons.put(aPerson.getName(), aPerson); <span class='comment'>//replaces old entry
</span>        result = <span class='keyword'>true</span>;
        log(<span class='literal'>"Persons after change: "</span> + fPersons);
      }
    }
    <span class='keyword'>return</span> result;
  }

  <span class='comment'>/**
  * Delete the &lt;tt&gt;Person&lt;/tt&gt; identified uniquely by &lt;tt&gt;aName&lt;/tt&gt;,
  * returning an indicator of success or failure.
  *
  * &lt;P&gt;If &lt;tt&gt;aName&lt;/tt&gt; is not found in the datastore, then return false.
  */</span>
  <span class='keyword'>public</span> <span class='keyword'>boolean</span> delete(String aName) <span class='keyword'>throws</span> DataAccessException {
    <span class='keyword'>boolean</span> result = <span class='keyword'>false</span>;
    <span class='keyword'>synchronized</span>(fPersons){
      <span class='keyword'>if</span> ( fPersons.containsKey(aName) ) {
        fPersons.remove(aName);
        result = <span class='keyword'>true</span>;
        log(<span class='literal'>"Persons after delete: "</span> + fPersons);
      }
    }
    <span class='keyword'>return</span> result;
  }

  <span class='comment'>// PRIVATE //
</span>
  <span class='comment'>/**
  * Stores Person objects in a static field, attached to this class, which will
  * remain available as long as this class remains loaded.
  *
  * &lt;P&gt;Keys - String containing the Person's name.&lt;br&gt;
  * Values - Person object.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> Map fPersons = <span class='keyword'>new</span> LinkedHashMap();

  <span class='keyword'>private</span> <span class='keyword'>void</span> log(String aText){
    System.out.println(aText);
  }
} 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicAction564b-2.html?Id=66'>Data access objects</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='200'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=200 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:29 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
